package com.cwiyc.orderonlineback.controller;

//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cwiyc.orderonlineback.common.Result;
//import com.cwiyc.orderonlineback.entity.Email;
import com.cwiyc.orderonlineback.entity.User;
import com.cwiyc.orderonlineback.mapper.UserMapper;
import com.cwiyc.orderonlineback.service.UserServiceImpl;
//import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
//import javax.mail.MessagingException;
import java.util.Date;

@RestController
@RequestMapping
public class UserController {

    @Resource
    UserMapper userMapper;

    @Autowired
    private UserServiceImpl userService;

    @PostMapping("/register")
    public Result<?> save(@RequestBody User user) {
        return userService.register(user);
    }

    @PostMapping("/login")
    public Result<?> info(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/upAvatar")
    public Result<?> upAvatar(@RequestBody User user) {
        return userService.updateAvatar(user);
    }

    @PostMapping("/getuser")
    public Result<?> getUsers(@RequestBody User user) {
        return userService.getuuser(user);
    }

    // 邮箱注册
//    @PostMapping("/regemail")
//    public Result regEmail(@RequestBody Email email) {
//        String ema = email.getEmail();
//        String code = email.getCode();
//        Integer type = email.getType();
//        Date expiretime=email.getExpiretime();
//        return userService.regEmail(ema,code,type,expiretime);
////        User ress = userMapper.getuser(res);
////        if (ress == null) {
////            return Result.error("-1", "邮箱注册的用户名不存在");
////        }
//    }
//
//    // 邮箱登录
//    @PostMapping("/loginemail")
//    public Result loginEmail(@RequestBody Email email) {
//        String ema = email.getEmail();
//        String code = email.getCode();
//        Integer type = email.getType();
//        Date expiretime=email.getExpiretime();
//        String res = userService.loginEmail(ema,code,type,expiretime);
//        if (res.equals("")) {
//            return Result.error("-1", "邮箱验证码错误或失效");
//        }
//        User ress = userMapper.getuser(res);
//        if (ress == null) {
//            return Result.error("-1", "邮箱注册的用户名不存在");
//        }
//        return Result.success(res);
//    }
//
//    // 发送邮箱验证码
//    @GetMapping("/email/{email}/{type}")
//    public Result sendEmailCode(@PathVariable String email, @PathVariable Integer type) throws MessagingException {
//        int res = userService.sendEmailCode(email, type);
//        if (res == 0) {
//            return Result.error("-1", "发送邮箱验证码失败");
//        }
//        return Result.success();
//    }
}
